#include "signlogin.h"
#include "ui_signlogin.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QMouseEvent>
#include <QDateTime>
#include <QDebug>

SignLogin::SignLogin(QWidget *parent) :
    QWidget(parent),
    m_socket(new MyClientSocket(this)),
    ui(new Ui::SignLogin)
{
    ui->setupUi(this);
     m_socket->connectToServer("120.26.230.219",8080);


     //设置窗口无边框
     setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
     setAttribute(Qt::WA_TranslucentBackground);

     connect(ui->loginBtn,&QPushButton::clicked,this,&SignLogin::handleLogin);
     connect(ui->registerBtn,&QPushButton::clicked,this,&SignLogin::handleRegiter);
     connect(m_socket,&MyClientSocket::dataRecv,this,&SignLogin::handleDataRecv);
}

SignLogin::~SignLogin()
{
//    if(m_socket != NULL)
//        delete  m_socket;
//    if(m_client != NULL)
//        delete  m_client;
    delete ui;
}

//界面拖动
void SignLogin::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)// 检查是否是左键
    {
        m_dragging = true;// 设置拖拽标志
        m_dragPosition = event->globalPos() - frameGeometry().topLeft();// 记录鼠标点击时相对于窗口左上角的位置
        event->accept();// 接受事件
    }
//    SetHeadPhoto(event);

}

//界面拖动
void SignLogin::mouseMoveEvent(QMouseEvent *event)
{
    if (m_dragging && (event->buttons() & Qt::LeftButton)) // 检查是否正在拖拽且鼠标左键按下
    {
        move(event->globalPos() - m_dragPosition); // 移动窗口到新的位置
        event->accept();// 接受事件
    }
}

//界面拖动
void SignLogin::mouseReleaseEvent(QMouseEvent *event)
{
    m_dragging = false;// 拖拽结束
}



void SignLogin::handleLogin()
{
    QString account = ui->account_Edit->text();
    QString passwd = ui->passwd_Edit->text();

    //1.创建json对象
    QJsonObject jsonObj;

    //2.设置<key:value>属性
    jsonObj["type"] = LOGIN;
    jsonObj["account"] = account;
    jsonObj["passwd"] = passwd;

    m_account = account;
    //3.创建jsonDocument对象
    QJsonDocument document(jsonObj);

    m_socket->writeBytes(document.toJson());
}

void SignLogin::handleRegiter()
{

    //1.创建json对象
    QJsonObject jsonObj;

    //2.设置<key:value>属性
    jsonObj["type"] = REGISTER;
    jsonObj["account"] = ui->account_Edit->text();
    jsonObj["passwd"] = ui->passwd_Edit->text();

    //3.创建jsonDocument对象
    QJsonDocument document(jsonObj);

    m_socket->writeBytes(document.toJson());
}

void SignLogin::handleDataRecv(const QByteArray &data)
{
    /* 解析json对象 */
    QJsonDocument document = QJsonDocument::fromJson(data);

    /* 将document对象转换成json对象 */
    QJsonObject jsonObj = document.object();

    int type = jsonObj["type"].toInt();

    if(type == REGISTER)
    {
        QString reason = jsonObj["reason"].toString();
        QMessageBox::information(this,"提示",reason);
    }
    else if(type == LOGIN)
    {

        QString status = jsonObj["status"].toString();

        if(status == "ON_SUCCESS")
        {
            QString encodedContent = jsonObj["pix"].toString();

            // 解码 Base64 内容
            QByteArray decodedContent = QByteArray::fromBase64(encodedContent.toUtf8());
            QPixmap pix;
            pix.loadFromData(decodedContent);
            QMessageBox::information(this, "登录状态", "登录成功");
            m_client = new Client(m_account,pix,m_socket);
            m_client->show();
            disconnect(m_socket,&MyClientSocket::dataRecv,this,&SignLogin::handleDataRecv);
            /*隐藏当前窗口*/
            this->hide();
        }
        else
        {
            {
                QString reason = jsonObj["reason"].toString();
                QMessageBox::information(this, "登录状态", "登录失败");
            }
        }
    }
}


void SignLogin::on_closeBtn_clicked()
{
    this->close();
}
